package com.sqx.modules.pay.controller.app;

import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.pay.dao.PayDetailsDao;
import com.sqx.modules.pay.service.WxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;

/**
 * @author fang
 * @date 2020/2/26
 */
@RestController
@Api(value = "用户微信支付", tags = {"用户微信支付"})
@RequestMapping("/app/wxPay")
@Slf4j
public class ApiWeiXinPayController {

    @Autowired
    private WxService wxService;
    @Autowired
    private PayDetailsDao payDetailsDao;


    @Login
    @ApiOperation("微信支付宝充值钱包")
    @PostMapping("/wxPayJsApiMoney")
    public Result wxPayJsApiMoney(@RequestAttribute("userId") Long userId, BigDecimal money, Integer type) throws Exception {
        return wxService.payMoney(userId,money,type);
    }

    @Login
    @ApiOperation("微信支付宝支付订单")
    @PostMapping("/wxPayJsApiOrder")
    public Result wxPayJsApiOrder(@RequestAttribute("userId") Long userId, Long parentId, Integer type, Long addressId, Integer orderType) throws Exception {
        return wxService.payOrder(userId,parentId,type, addressId, orderType);
    }

    @Login
    @ApiOperation("余额支付订单")
    @PostMapping(value = "balanceOrder")
    public Result balanceOrder(@RequestAttribute Long userId, Long parentId, Integer orderType, Long addressId){

        return wxService.balanceOrder(userId, parentId, orderType, addressId);
    }

    @Login
    @ApiOperation("余额购买会员")
    @PostMapping(value = "balanceBuyVip")
    public Result balanceBuyVip(@RequestAttribute Long userId){

        return wxService.balanceBuyVip(userId);
    }

    @Login
    @ApiOperation("微信支付宝购买会员")
    @PostMapping(value = "wxPayJsApiBuyVip")
    public Result wxPayJsApiBuyVip(@RequestAttribute Long userId, Integer type) throws Exception {

        return wxService.wxPayJsApiBuyVip(userId, type);
    }


    @PostMapping("/notify")
    @ApiOperation("用户端app微信回调")
    public String wxPayNotify(HttpServletRequest request) {
        return notify(1,request);
    }

    @PostMapping("/notifyJsApi")
    @ApiOperation("用户端小程序微信回调")
    public String notifyJsApi(HttpServletRequest request) {
        return notify(3,request);
    }

    @PostMapping("/notifyMp")
    @ApiOperation("用户端公众号微信回调")
    public String notifyMp(HttpServletRequest request) {
        return notify(2,request);
    }

    @PostMapping("/notifyShop")
    @ApiOperation("商户端app微信回调")
    public String notifyShop(HttpServletRequest request) {
        return notifyShop(1,request);
    }

    @PostMapping("/notifyJsApiShop")
    @ApiOperation("商户端小程序微信回调")
    public String notifyJsApiShop(HttpServletRequest request) {
        return notifyShop(3,request);
    }

    @PostMapping("/notifyMpShop")
    @ApiOperation("商户端公众号微信回调")
    public String notifyMpShop(HttpServletRequest request) {
        return notifyShop(2,request);
    }



    public String notify(Integer type,HttpServletRequest request){
        String resXml = "";
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                log.info(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = wxService.payBack(resXml,type);
            log.info("成功");
            log.info(result);
            return result;
        } catch (Exception e) {
            log.info("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            log.info("失败");
            log.info(result);
            return result;
        }
    }


    public String notifyShop(Integer type,HttpServletRequest request){
        String resXml = "";
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                log.info(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = wxService.payBackShop(resXml,type);
            log.info("成功");
            log.info(result);
            return result;
        } catch (Exception e) {
            log.info("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            log.info("失败");
            log.info(result);
            return result;
        }
    }

}
